package logic

import (
	"context"
	"fmt"
	"newuser/models"

	"newuser/newuserrpc/internal/svc"
	"newuser/newuserrpc/newuserrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NewUserRrcLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNewUserRrcLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NewUserRrcLoginLogic {
	return &NewUserRrcLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *NewUserRrcLoginLogic) NewUserRrcLogin(in *newuserrpc.NewUserRrcLoginRequest) (*newuserrpc.NewUserRrcLoginResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	models.DB.Debug().Where("username=?", in.Username).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在")
	}
	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误,请重新输入")
	}
	return &newuserrpc.NewUserRrcLoginResponse{
		Id: int64(user.ID),
	}, nil
}
